package com.sskj.mqtt.many;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

/**
 * @Author: Like
 * @Date: 2025/3/3 15:40
 * @Description: MqttListener注解扫描器
 * 在Bean初始化后扫描所有带有@MqttListener注解的方法，并自动注册到MqttClientCallback中
 */
@Component
public class MqttListenerScanner implements BeanPostProcessor {
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        // 扫描所有Bean的方法
        for (Method method : bean.getClass().getDeclaredMethods()) {
            MqttListener annotation = method.getAnnotation(MqttListener.class);
            if (annotation != null) {
                // 自动注册到处理器映射
                MqttClientCallback.registerAnnotatedMethod(bean, method, annotation);
            }
        }
        return bean;
    }
}
